package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/3/1 10:03
 * @description
 */
public class Code141 {
    public static void main(String[] args) {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        n1.next = n2;
        n2.next = n1;
        System.out.println(new Code141().hasCycle(n1));
    }

    private List<ListNode> l = new ArrayList<>();
    private boolean flag = false;

    public boolean hasCycle(ListNode head) {
        isCycle(head);
        return flag;
    }

    private void isCycle(ListNode node) {
        if (node != null) {
            if (l.contains(node)) {
                flag = true;
                return;
            }
            l.add(node);
            isCycle(node.next);

        }

    }

}
